#ifndef USART_H
#define USART_H

// USART operation header

#include "stm32f10x_usart.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "misc.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "../dma/dma.h"

#define USART_RX_Buffer_Length (uint8_t)256 // USART Receive Buffer Length(蓝牙接收缓冲区长度)
#define USART_TX_Buffer_Length (uint8_t)256 // USART Transmit Buffer Length(蓝牙发送缓冲区长度)

#define USART_RX_End_Char '\0' // 结尾字符

// USART Buffer Structure(蓝牙缓冲区结构体)
typedef struct USART_Buffer
{
    uint8_t USART_RX_DataSlice;                            // USART Receive Data Slice(蓝牙接收数据切片)
    uint8_t USART_TX_DataSlice;                            // USART Transmit Data Slice(蓝牙发送数据切片)
    uint8_t USART_RXFlag;                                  // USART Receive Flag(蓝牙接收标志)
    uint8_t USART_TXFlag;                                  // USART Transmit Flag(蓝牙发送标志)
    uint8_t USART_RX_Buffer[USART_RX_Buffer_Length];       // USART Receive Buffer(蓝牙接收缓冲区)
    uint8_t USART_RX_Buffer_Slice[USART_RX_Buffer_Length]; // USART Receive Buffer Slice(蓝牙接收缓冲区切片)
    uint8_t USART_TX_Buffer[USART_TX_Buffer_Length];       // USART Transmit Buffer(蓝牙发送缓冲区)
    uint8_t USART_TX_Buffer_Slice[USART_TX_Buffer_Length]; // USART Transmit Buffer Slice(蓝牙发送缓冲区切片)
    uint8_t USART_RX_Index;                                // USART Receive Index(蓝牙接收索引)
    uint8_t USART_TX_Index;                                // USART Transmit Index(蓝牙发送索引)
    uint8_t USART_RX_Complete;                             // USART Receive Complete(蓝牙接收完成标志)
    uint8_t USART_TX_Complete;                             // USART Transmit Complete(蓝牙发送完成标志)
} USARTBuffer;

void USART_SendByte(USART_TypeDef *usart, uint8_t DataByte);                                    // Send a Byte(发送一个字符)
void USART_SendArray(USART_TypeDef *usart, uint8_t *Array, uint8_t Length);                     // Send an Array(发送一个数组)
void USART_SendString(USART_TypeDef *usart, char *String);                                      // Send a String(发送一个字符串)
uint32_t USART_Pow(uint32_t x, uint32_t pow);                                                   // Power(幂)
void USART_SendNumber(USART_TypeDef *usart, uint32_t Number, uint8_t Length);                   // Send a Number(发送一个数字)
void USART_Printf(USART_TypeDef *usart, char *format, ...);                                     // Printf by self(自定义Printf)                                                                  // Printf by STM32(STM32自带Printf)
USART_TypeDef *getUSARTPortTypeDef(uint16_t port_tx, uint16_t port_rx, GPIO_TypeDef *gpio_pin); // Get USART Port(获取USART端口)
void clearUSARTBuffer(USART_TypeDef *usart);                                                    // Clear USART Buffer(清空USART缓冲区)

// test port
void TestUASRT_Init(uint32_t baudRate);
void USART2_IRQHandler(void);

#endif